import React from 'react';
import { inject, observer } from 'mobx-react';
import { Button,  } from 'antd';
import {toJS} from "mobx";
import {WeaTableNew} from 'comsMobx';
const WeaTable = WeaTableNew.WeaTable;
import {WeaRightMenu,WeaTop,WeaTab,WeaErrorPage,WeaTools} from 'ecCom';
import {Condition,getAdButtons} from '../list/listCondition';
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

@inject('cptFlowConditionStore')
@observer
class FlowConditionResult extends React.Component{
    constructor(props){
        super(props);
    }
    componentWillMount(){
        const {cptFlowConditionStore} = this.props;
        cptFlowConditionStore.doSearch();
    }
    componentWillUnmount(){
        const {cptFlowConditionStore} = this.props;
        cptFlowConditionStore.clearStatus();
    }
    render(){
        const {cptFlowConditionStore} = this.props;
        const {loading,form,showSearchAd,tableStore,reLoad} = cptFlowConditionStore;
        const title = getLabel( 1501 ,"流转情况");
        return (
            <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@ak8rbh`} datas={this.getRightMenu()}   onClick={this.onRightMenuClick.bind(this)}  
            collectParams={{
                favname:'',
                favouritetype: "5",
                objid: 0,    
                importantlevel:1,
              }}>
                <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@s668a4`}
                    title={title}
                    loading={loading}
                    icon={<i className='icon-coms-fa' />}
                    iconBgcolor='#f14a2d'
                    buttons={this.getTopButtons()}
                    buttonSpace={10}
                    showDropIcon={true}
                    dropMenuDatas={this.getRightMenu()}
                    onDropMenuClick={this.onRightMenuClick.bind(this)} 
                >
                    <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@rhq6d9`}
                        buttonsAd={getAdButtons(cptFlowConditionStore)}
                        searchType={['advanced']}
                        setShowSearchAd={bool=>{cptFlowConditionStore.setShowSearchAd(bool)}}
                        hideSearchAd={()=> cptFlowConditionStore.setShowSearchAd(false)}
                        searchsAd={
                            <div><Condition ecId={`${this && this.props && this.props.ecId || ''}_Condition@kvks3d`} listStore={cptFlowConditionStore} form={form} onEnterSearch={this.onEnterSearch}></Condition></div>
                        }
                        showSearchAd={showSearchAd}
                        onSearch={v=>{prjExecuteStore.doSearch()}}
                    />
                    <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@iwcjbq`} 
                        comsWeaTableStore={tableStore}
                        hasOrder={true}
                        needScroll={true}
                        register_table={()=>reLoad()}
                    />
                </WeaTop>
            </WeaRightMenu>
        )
    }
    getTopButtons(){
        const {cptFlowConditionStore} = this.props;
        const {rightMenu} = cptFlowConditionStore;
       let btnArr = [];
       let that = this;
       rightMenu && rightMenu.length>0 && toJS(rightMenu).map(m=>{
           m.isTop == '1' && btnArr.length < 4 && btnArr.push(
               <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@vvguhx@${m.type}`} type="primary" 
                   onClick={()=>{
                        if(m.type == "BTN_IMPORT_EXCEL"){ //导出Excel
                            cptFlowConditionStore.cptFlowExport();
                        }else if(m.type == "BTN_SEARCH"){
                            cptFlowConditionStore.doSearch();
                        }
                   }}>
                   {m.menuName}
               </Button>
           );
       });
       return btnArr;
    }
    getRightMenu(){
        const {cptFlowConditionStore} = this.props;
        const {rightMenu} = cptFlowConditionStore;
        let btnArr = [];
        rightMenu && rightMenu.length>0 && rightMenu.map(m=>{
            btnArr.push({
                icon: <i className={m.menuIcon} />,
                content: m.menuName,
            })
        });
    	return btnArr
    }
    onRightMenuClick(key){
        const {cptFlowConditionStore }= this.props;
        const {rightMenu} = cptFlowConditionStore;
        rightMenu &&  rightMenu.length>0  && rightMenu.map((m,i)=>{
        	if(Number(key) == i){
                if(m.type == "BTN_SEARCH"){ //搜索
                    cptFlowConditionStore.doSearch();
                }else if(m.type == "BTN_IMPORT_EXCEL"){ // 导出Excel
                    cptFlowConditionStore.cptFlowExport();
                }
            }
        });
    }

    onEnterSearch = () =>{
        const {cptFlowConditionStore }= this.props;
        cptFlowConditionStore.doSearch();
        cptFlowConditionStore.setShowSearchAd(false);
    }

}

export default WeaTools.tryCatch(React,
    props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@b7u098`} msg={ props.error ? props.error : getLabel( 383324,'对不起，该页面异常，请联系管理员' )} />,
    {error: ""}
)(FlowConditionResult);